﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class DispatchNoteControl : UserControl
    {
        Product prod = new Product();
        Users users = new Users();
        Users ActiveUser = Program.GetActiveUser;
        Partner partner = new Partner();
        public DispatchNoteControl()
        {
            Program.GetMainForm.Text = "TMS - Испратници";
            InitializeComponent();
            dgvDispatchList.DataSource = prod.GetAllDispatchNotes().Tables[0];
            dgvDispatchList.Columns[0].Visible = false;
            dgvDispatchList.Columns[1].HeaderText = "Датум";
            dgvDispatchList.Columns[2].HeaderText = "Одговорен";
            dgvDispatchList.Columns[3].HeaderText = "Испратница бр.";
            dgvDispatchList.Columns[3].DisplayIndex = 1;
            dgvDispatchList.Columns[4].HeaderText = "Коминтент";
            dgvDispatchList.Columns[5].HeaderText = "Статус";
            //dgvDispatchList.Columns[5].DisplayIndex = 4;
            cmbPartners.ValueMember = "name";
            //cmbResponsible.DataSource = .Tables[0];
           // MessageBox.Show(users.GetAllUserNameAndSurname().Tables[0].Rows[0].ItemArray[0].ToString());
            //cmbResponsible.ValueMember = "namesurname";
            FillResponsibels();
            FillPartners();
        }

        public void FillPartners()
        {
            DataTable table = partner.GetAllPartners();
            cmbPartners.DataSource = table;
        }

        public void FillResponsibels()
        {
            DataTable table = users.GetAllUserNameAndSurname();
            foreach (DataRow row in table.Rows)
            {
                cmbResponsible.Items.Add(row.ItemArray[0].ToString());
                cmbResponsible.Items.Remove("Администратор ");
                cmbResponsible.SelectedItem = ActiveUser.GetName() + " " + ActiveUser.GetSurname();
            }
        }

       

        

        private void dgvDispatchList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_DISPATCH_NOTE_CONTROL, dgvDispatchList.CurrentRow);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_DISPATCH_NOTE_CONTROL, dgvDispatchList.CurrentRow);
            this.Dispose();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано испратница");
            else
            {
                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворена")
                    MessageBox.Show("Испратницата е веќе затворена");
                else
                {
                    prod.CloseDispatchNote(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                    this.Dispose();
                    Program.GetMainForm.ShowControl(ControlsEnum.DISPATCH_NOTE_CONTROL);
                }
            }
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        private void ToStNew_Click(object sender, EventArgs e)
        {
            grNew.Visible = true;
        }

        private void ToStSave_Click(object sender, EventArgs e)
        {
            prod.AddDispatchNote(prod.GetLastDispatchNote() + 1, cmbResponsible.SelectedItem.ToString(), dtpDate.Value, cmbPartners.SelectedValue.ToString(), "Отворена");
            this.Dispose();
            Program.GetMainForm.ShowControl(ControlsEnum.DISPATCH_NOTE_CONTROL);
        }

        private void ToStClose_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано испратница");
            else
            {
                if (dgvDispatchList.SelectedRows[0].Cells[5].Value.ToString() == "Затворена")
                    MessageBox.Show("Испратницата е веќе затворена");
                else
                {
                    prod.CloseDispatchNote(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                    this.Dispose();
                    Program.GetMainForm.ShowControl(ControlsEnum.DISPATCH_NOTE_CONTROL);
                }
            }
        }
    }
}
